package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

/**
 * @Author tang.qingbo
 * @Date 2020/8/30 15:35
 * @Version 1.0
 */
@Controller
@RequestMapping("/test")
public class TestController {
    /**
     * 跳转demo1.html
     *
     * @param model
     * @return
     */
    @RequestMapping("/hello")
    public String hello(Model model) {
        model.addAttribute("hello", "hello world!");
        model.addAttribute("description", "<span>张三</span>");
        //默认配置，视图前缀：classpath:/templates/,视图后缀：.html

        List<User> userList = new ArrayList<>();
        userList.add(new User(1, "张三", "深圳"));
        userList.add(new User(2, "李四", "安徽"));
        userList.add(new User(3, "王五", "安庆"));
        model.addAttribute("userList", userList);

        model.addAttribute("nowDate", new Date());

        model.addAttribute("age", 24);

        //map
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("No", "123");
        dataMap.put("address", "深圳");
        model.addAttribute("dataMap", dataMap);

        //字符串处理
        model.addAttribute("name", "spec_张三");

        //添加地址
        model.addAttribute("url", "/test/add");
        return " ";
    }

    /**
     * 接收前端传递数据
     */
    @RequestMapping("/add")
    public String add(String name, String address, Model model) {
        System.out.println("name: " + name + " address: " + address);
        return "redirect:http://www.itheima.com";
    }
}
